#!/bin/bash
##这是编写Dockerfile的脚本
##提供lap环境，MySQL服务通过连接外部实现




echo ###-----------------------判断是否存在centos镜像------------##
if [ -z "$(docker images|grep docker.io/centos)"]
then
	docker pull centos
else
	echo 开始配置相关参数
fi

echo ###----------------开始设置参数------------------------#####

echo 请选择网站源码压缩包的格式
echo 1:通用格式zip
echo 2:通用格式tar.gz
read ym
echo 请输入源码URL地址
read discuzurl
echo 请输入网站端口
read port
echo 请输入容器名称
read con

echo ##------------------开始编辑Dockerfile文件--------------###
echo "FROM centos" >> configure/Dockerfile
echo "RUN yum install -y httpd  wget unzip python2-PyMySQL expect" >> configure/Dockerfile
echo "RUN yum install -y php-fpm php-* --skip-broken to work around the problem" >> configure/Dockerfile
echo "RUN sed -i 's/#ServerName www.example.com:80/ServerName localhost:80/g' /etc/httpd/conf/httpd.conf" >> configure/Dockerfile


if [ $ym == 1 ]
then
	mkdir /opt/ym
	wget -O ym.zip $discuzurl /opt/ym/
	unzip ym.zip 
	rm -rf ym.zip
elif [ $ym == 2 ]
then
	mkdir /opt/ym
        wget -O ym.tar.gz $discuzurl /opt/ym/
        tar -zxvf ym.tar.gz
	rm -rf ym.tar.gz

else
	echo 选择错误
	rm -rf configure/Dockerfile
fi
echo "RUN echo systemctl enable php-fpm >> /root/start.sh" >> configure/Dockerfile
echo "RUN echo systemctl enable httpd >> /root/start.sh" >> configure/Dockerfile
echo "RUN echo systemctl restart httpd >> /root/start.sh" >> configure/Dockerfile
echo "RUN echo systemctl restart php-fpm >> /root/start.sh" >> configure/Dockerfile


echo ##------------------------开始执行镜像构建------------------------#####
docker build -t discuz:v1.0 .
echo ##-------------------------惊喜构建完成---------------------------#####
rm -rf Dockerfile
echo ##----------------------开始创建容器------------------------------####
docker run -itd --privileged -p $port:80 --name $con $(docker images | grep none|awk '{print $3}') /usr/sbin/init

echo ##------------------------开始编辑服务启动脚本-------------------##
docker ps -a

echo "systemctl enable php-fpm"  >> /etc/start.sh
echo "systemctl restart php-fpm" >> /etc/start.sh
echo "systemctl enable httpd"  >> /etc/start.sh
echo "systemctl restart httpd"  >> /etc/start.sh
docker cp /root/start.sh $con:/root/start.sh
docker cp /opt/ym/* $con:/var/www/html/
docker exec $con bash -c "sh /root/start.sh"

